<template>

  <div class="userTable">
    <el-input
        v-model="keyword"
        placeholder="请输入用户名"
        class="input-with-select" style="margin: 20px auto">
      <template #append>
        <el-button @click="getUserByKey(keyword)">搜索</el-button>
      </template>
    </el-input>
    <el-table :data="userList" style="width: 100%">
      <el-table-column prop="nickname" label="昵称" width="180"/>
      <el-table-column prop="username" label="用户名" width="180"/>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <span v-if="scope.row.status=='1'">正常</span>
          <span v-else>封禁</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope1">
          <el-button type="danger" size="small" v-if="scope1.row.status==1" @click="banUser(scope1.row.id)">封禁
          </el-button>
          <el-button type="primary" size="small" v-else @click="allowUser(scope1.row.id)">解封</el-button>
          <el-button type="danger" size="small" @click="deleteUser(scope1.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import {getListUser, banUserById, cancelBanuserById, deleteUserById,getUserByKeyword} from "@/apis/user";
import {onMounted} from "vue";
import {ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";

const keyword = ref('')
const getUserByKey = async (keyword) => {
  const res = await getUserByKeyword(keyword)
  if(res.code === 200){
    ElMessage({type:'success',message:res.message})
    userList.value = res.data
  }else {
    ElMessage({type:'error',message:res.message})
    getAllUser()
  }
}

const userList = ref([])
const getAllUser = async () => {
  await getListUser().then(res => userList.value = res.data)
}
console.log(userList)
onMounted(() => {
  getAllUser()
})

const banUser = async (userid) => {
  await banUserById(userid).then(res => {
    if (res.code == 200) {
      ElMessage({type: 'success', message: '封禁成功'})
      getAllUser()
    } else {
      ElMessage({type: 'warn', message: '封禁失败'})
    }

  })
}

const allowUser = async (userid) => {
  await cancelBanuserById(userid).then(res => {
    if (res.code == 200) {
      ElMessage({type: 'success', message: '解封成功'})
      getAllUser()
    } else {
      ElMessage({type: 'warn', message: '解封失败'})
    }
  })
}
const deleteUser = async (userid) => {
  ElMessageBox.confirm(
      '你确定删除该信息吗?',
      'Warning',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        await deleteUserById(userid).then(res => {
          console.log(res)
          if (res.code == 200) {
            ElMessage({
              type: 'success',
              message: '删除成功',
            })
            getAllUser()
          } else {
            ElMessage({
              type: 'error',
              message: '删除失败',
            })
          }
        })
      }).catch(() => {
    ElMessage({type: 'info', message: '操作取消'})
  })
}
</script>
<style scoped>
.userTable {
  width: 800px;
  margin: 100px auto;
}
</style>